package com.example.acer.im.frags.media;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

import com.example.acer.im.R;
import com.example.common.utils.UiTool;
import com.example.common.common.widget.GalleryView;


/**
 * Created by HongQian.Wang on 2017/9/19.
 * 图片选择Fragment
 */

public class GalleryFragment extends BottomSheetDialogFragment implements GalleryView.SelectedChangeListener{
   private GalleryView mGallery;
   private  OnSelectedListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new TransStatusBottomShiftDialog(getContext());
    }

    @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          View root = inflater.inflate(R.layout.fragment_gallery, container, false);
          mGallery = (GalleryView) root.findViewById(R.id.gallery_view);
          return root;
      }

    @Override
    public void onStart() {
        super.onStart();
        mGallery.setup(getLoaderManager(),this);
    }

    @Override
    public void onSelectedCountChanged(int count) {
        //如果选中一张图片
              if(count>0) {
                  dismiss();//隐藏自己
                  if(listener !=null) {
                      String[] paths = mGallery.getSelectedPath();
                       listener.onSelectedImage(paths[0]);
                       listener=null;//取消和唤起者之间的引用,加快内存回收
                  }
              }
    }

    public interface  OnSelectedListener{
        Void onSelectedImage(String path);
    }

    /**\
     * 设置事件监听 并返回自己
     * @param listener
     * @return
     */
    public GalleryFragment setListener(OnSelectedListener listener){
         this.listener=listener;
         return this;
    }

    public   static class TransStatusBottomShiftDialog extends BottomSheetDialog{

        public TransStatusBottomShiftDialog(@NonNull Context context) {
            super(context);
        }

        public TransStatusBottomShiftDialog(@NonNull Context context, int theme) {
            super(context, theme);
        }

        protected TransStatusBottomShiftDialog(@NonNull Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
        }


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            Window window = getWindow();
            if(window==null) {
                return;
            }
            //得到屏幕高度
            int screenHeight = UiTool.getScreenHeight(getOwnerActivity());
         int statusHeight = UiTool.getStatusBarHeight(getOwnerActivity());

         int dialogHeight =screenHeight -statusHeight;
         window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT,
                 dialogHeight<=0?ViewGroup.LayoutParams.MATCH_PARENT:dialogHeight);
        }
    }


}
